package top.xia17.plugins.fast.common.database.redis;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * RedisTempFactory
 * @author xia17
 * @date 2020/5/7
 */
public class RedisTemplateFactory {

    /**
     * redisTemplate<String,Object> 实例
     * 使用jackson 序列化
     * @param redisConnectionFactory redis 链接工程
     * @param jacksonSerial 序列化方式
     * @param enableTransactionSupport 是否支持事务
     * @return redis 模板
     */
    public static RedisTemplate<String, Object> redisStrTemplate(RedisConnectionFactory redisConnectionFactory, Jackson2JsonRedisSerializer<Object> jacksonSerial, boolean enableTransactionSupport){
        if (jacksonSerial == null){
            jacksonSerial = xia17DefaultJacksonSerial();
        }
        //新建一个RedisTemplate
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        //配置连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //设置Value类型的 key-value 序列化方式
        redisTemplate.setValueSerializer(jacksonSerial);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // 设置hash key 和value序列化模式
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(jacksonSerial);
        // 开启事务
        redisTemplate.setEnableTransactionSupport(enableTransactionSupport);
        return redisTemplate;
    }

    /**
     * redisTemplate<String,Object> 实例
     * 使用jackson 序列化
     * @param redisConnectionFactory redis 链接工程
     * @param enableTransactionSupport 是否支持事务
     * @return redis 模板
     */
    public static RedisTemplate<String, Object> redisStrTemplate(RedisConnectionFactory redisConnectionFactory,  boolean enableTransactionSupport){
        return redisStrTemplate(redisConnectionFactory,null,enableTransactionSupport);
    }


    /**
     * redisTemplate<Object,Object> 实例
     * 使用jackson 序列化
     * @param redisConnectionFactory redis 链接工厂
     * @param jacksonSerial 序列化方式
     * @param enableTransactionSupport 是否支持事务
     * @return redis 模板
     */
    public static RedisTemplate<Object, Object> redisObjTemplate(RedisConnectionFactory redisConnectionFactory, Jackson2JsonRedisSerializer<Object> jacksonSerial, boolean enableTransactionSupport){
        if (jacksonSerial == null){
            jacksonSerial = xia17DefaultJacksonSerial();
        }
        if (redisConnectionFactory instanceof LettuceConnectionFactory){
            /* 关闭共享连接 ，redis 集群时需要开启
               如果不关闭在使用选库的时候会报错
               Selecting a new database not supported due to shared connection. Use separate Connection Factory to work with multiple databases
               由于共享连接，选择不支持的新数据库。使用单独的Connection Factory处理多个数据库  */
            ((LettuceConnectionFactory) redisConnectionFactory).setShareNativeConnection(false);
        }
        //新建一个RedisTemplate
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        //配置连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //设置Value类型的 key-value 序列化方式
        redisTemplate.setValueSerializer(jacksonSerial);
        redisTemplate.setKeySerializer(jacksonSerial);
        // 设置hash key 和value序列化模式
        redisTemplate.setHashKeySerializer(jacksonSerial);
        redisTemplate.setHashValueSerializer(jacksonSerial);
        redisTemplate.afterPropertiesSet();
        redisTemplate.setEnableTransactionSupport(enableTransactionSupport);
        return redisTemplate;
    }

    /**
     * redisTemplate<Object,Object> 实例
     * 使用jackson 序列化
     * @param redisConnectionFactory redis 链接工厂
     * @param enableTransactionSupport 是否支持事务
     * @return redis 模板
     */
    public static RedisTemplate<Object, Object> redisObjTemplate(RedisConnectionFactory redisConnectionFactory, boolean enableTransactionSupport){
        return redisObjTemplate(redisConnectionFactory,null,enableTransactionSupport);
    }


    /**
     * 使用jackson来序列化redis的value值
     * jackson Redis 序列化实例
     * @return Jackson2JsonRedisSerializer
     */
    public static Jackson2JsonRedisSerializer<Object> xia17DefaultJacksonSerial(){
        //objectMapper指定怎么序列化
        ObjectMapper objectMapper = new ObjectMapper();
        //指定要序列化的域，field,get和set,以及修饰符范围，ANY是都有包括private和public
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 这一句必须要，作用是序列化时将对象全类名一起保存下来,如果没有这个反序列化时对象类型都会丢失，成为HashMap
        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
        return new Jackson2JsonRedisSerializer<>(objectMapper , Object.class);
    }




}
